package com.hongxun.design.activitys;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.bean.CheakBean;
import com.hongxun.design.bean.ProjectItemBean;
import com.hongxun.design.interfaces.HeadSecond;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.IntentToActivity;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.MyLog;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.utils.DataUtil;
import com.hongxun.design.utils.fastjson.FastJsonUtil;
import com.hongxun.design.utils.httputil.HttpStringUtil;

/**
 * Created by zerdoor_pc on 2016/6/24.
 * 项目自检
 */
public class ProjectSelfCheckActivity extends BaseActivity implements HeadSecond {
    RelativeLayout projectname_button, bit_pop_item;
    TextView bittype_right, self_right, self_radius, self_Span, self_connrelation, self_configuration, self_no;
    static TextView projectname;
    LinearLayout self_value;
    static ProjectItemBean Project;
    private static Handler checkProjectHandler;
    RelativeLayout self_radius_rl, self_Span_rl, self_connrelation_rl, self_configuration_rl;
    public String type = "11";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_projectselfcheck);
        Project = new ProjectItemBean();
        findBar();
        findView();
    }

    @Override
    public void findBar() {
        HeadSecondControlPanel headsecond_layout = (HeadSecondControlPanel) findViewById(R.id.projectselfcheck_layout);
        headsecond_layout.initHeadPanel(DataUtil.getsecondbean("", View.GONE, R.color.bar_right, ListenerSet.back, "数据检查", R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void findView() {
        super.findView();
        TextView button_bottom = (TextView) findViewById(R.id.projectselfcheck_button);
        self_value = (LinearLayout) findViewById(R.id.self_value);
        SetTypeface.set(button_bottom, IconSourse.selfcheck);
        button_bottom.setText(" 开始检查");


        //新建点  取消
        findViewById(R.id.bit_pop).setVisibility(View.GONE);

        bittype_right = (TextView) findViewById(R.id.bittype_right);


        //项目选择
        projectname_button = (RelativeLayout) findViewById(R.id.self_projectname_button);
        projectname = (TextView) findViewById(R.id.self_projectname);
        self_right = (TextView) findViewById(R.id.self_right);

        SetTypeface.set(self_right, IconSourse.change);
        choiceProject();

        /**初始化显示值**/

        self_radius_rl = (RelativeLayout) findViewById(R.id.self_radius_rl);
        self_Span_rl = (RelativeLayout) findViewById(R.id.self_Span_rl);
        self_connrelation_rl = (RelativeLayout) findViewById(R.id.self_connrelation_rl);
        self_configuration_rl = (RelativeLayout) findViewById(R.id.self_configuration_rl);
        self_no = (TextView) findViewById(R.id.self_no);
        change(View.GONE, View.VISIBLE);
        //自检值
        self_radius = (TextView) findViewById(R.id.self_radius);
        self_Span = (TextView) findViewById(R.id.self_Span);
        self_connrelation = (TextView) findViewById(R.id.self_connrelation);
        self_configuration = (TextView) findViewById(R.id.self_configuration);
        addCheckProjectHandler();
    }

    private void change(int value, int novalue) {
        self_radius_rl.setVisibility(View.GONE);
        self_Span_rl.setVisibility(View.GONE);
        self_connrelation_rl.setVisibility(value);
        self_configuration_rl.setVisibility(value);
        self_no.setVisibility(novalue);
    }

    private void setSelf_Result(CheakBean bean) {
        if (bean.joinCon.list.length > 0) {
            SetTypeface.set(self_connrelation, IconSourse.WRONG);
            self_connrelation.append(" " + bean.joinCon.result);
            self_connrelation.setBackgroundResource(R.drawable.redline3_wihte_item);
            self_connrelation.setTextColor(getResources().getColor(R.color.project_red_point));
            self_connrelation.setOnClickListener(new MClickListener(2, bean.joinCon.list) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    //连接关系
                    intentToMore((String[]) msg, "连接关系");
                }
            });
        } else {
            self_connrelation.setText(bean.joinCon.result);
            self_connrelation.setTextColor(getResources().getColor(R.color.project_true));
            self_connrelation.setBackgroundResource(R.color.transparent);
        }

        if (bean.codeset.list.length > 0) {
            SetTypeface.set(self_configuration, IconSourse.WRONG);
            self_configuration.append(" " + bean.codeset.result);
            self_configuration.setBackgroundResource(R.drawable.redline3_wihte_item);
            self_configuration.setTextColor(getResources().getColor(R.color.project_red_point));
            self_configuration.setOnClickListener(new MClickListener(3, bean.codeset.list) {
                @Override
                public void onClick(View view, int position, Object msg) {
                    //典设配置
                    intentToMore((String[]) msg, "典设配置");
                }
            });
        } else {
            self_configuration.setText(bean.codeset.result);
            self_configuration.setTextColor(getResources().getColor(R.color.project_true));
            self_configuration.setBackgroundResource(R.color.transparent);
        }

    }

    private void intentToMore(String[] list, String title) {
        MyLog.L(MString.SUCCESSED + "sd=" + JSON.toJSONString(list));
        if (list.length == 0) return;
        String value = "";
        for (int i = 0; i < list.length; i++) {
            try {
                JSONObject object = JSON.parseObject(list[i]);
                value += object.getString(MString.number) + "(" + object.getString(MString.name) + "),";
            } catch (Exception e) {
            }
        }
        value = value.substring(0, value.length() - 1) + "。";
        Bundle bundle = new Bundle();
        bundle.putString(MString.CODE, value);
        bundle.putString(MString.TITLE, title);
        IntentToActivity.intent(this, CodeLibraryActivity.class, bundle);
    }

    /**
     * 开始检查*
     */
    public void checkSelf(View v) {
        if (Project.getProject_name().length() == 0) {
            ToastShow.showShort(mActivity, "项目不能为空！");
            return;
        }
        checkProject();
    }

    private void checkProject() {
        MyApp.HTTP.checkProject(checkProjectHandler, HttpStringUtil.SUCCESSED, Project.getStage_id(), type);
    }

    private void addCheckProjectHandler() {
        checkProjectHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HttpStringUtil.SUCCESSED:
                        String s = FastJsonUtil.JsonToCheck_String((String) msg.obj);
                        if (FastJsonUtil.JsonToCheck((String) msg.obj) && !"{}".equals(msg.obj)) {
                            CheakBean bean = FastJsonUtil.JsonToGetcheckProject((String) msg.obj);
                            setSelf_Result(bean);
                            change(View.VISIBLE, View.GONE);
                        } else {
                            ToastShow.showShort(mActivity, s);
                        }
                        break;
                    case 1:
                        change(View.GONE, View.VISIBLE);
                        break;
                    case HttpStringUtil.ERROR:
                        break;
                }
            }
        };
    }

    /**
     * 设置点位类型*
     */
//    private void setTypeChoice() {
//        SetTypeface.set(bittype_right, IconSourse.DROPDOWN);
//        bit_pop.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                MyLog.L(TAG + "点击类型选择器");
//                PopwindowUtil.openBitPop(view, self_value, mActivity);
//            }
//        });
//    }

    /**
     * 项目选择*
     */
    private void choiceProject() {
        projectname.setText("选择项目");
        projectname_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString(MString.TYPE, MString.projectChoice_selfcheck);
                IntentToActivity.intent(mActivity, CreateProjectAttributeCodeActivity.class, bundle);
            }
        });
    }


    /**
     * 更新选择项目*
     */
    public static void setValue(ProjectItemBean bean) {
        MyLog.L(MString.SUCCESSED + " bean=" + JSON.toJSONString(bean));
        Project.clearBean();
        Project.setBean(bean, 1);
        MyLog.L(MString.SUCCESSED + " bean=" + JSON.toJSONString(bean));
        MyLog.L(MString.SUCCESSED + " Project.getProject_name()=" + Project.getProject_name());
        projectname.setText(Project.getProject_name());
        checkProjectHandler.sendEmptyMessage(1);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Project.clearBean();
        Project = null;
        projectname = null;
        checkProjectHandler = null;
    }
}
